In [6]:
from XQuant import EnhancingDividend
from XQuant import BackTestOptions, BackTestRunner, Strategy, Formatter
from XQuant import TradeDate
import pandas as pd
import numpy as np

begin = "20220101"
bench_code = "000300"
ed  = EnhancingDividend(begin=begin)
In [2]:
opts = BackTestOptions(
    begin = "20230101",
    surname = "增强红利因子",
    bench_code = bench_code,
    method = Strategy.GROUP,
    direction = 1,
    quantile = 0.05,
    # clean_cache = True
)
In [3]:
bench = pd.read_csv("沪深300指数历史数据.csv", index_col="日期", usecols=["日期", "涨跌幅"], parse_dates=True)
bench.index.name = "date"
bench = bench.rename(columns={"涨跌幅":"returns"})
bench.returns = bench.returns.str.strip("%").astype(float) / 100
bench = bench.sort_index()
bench
Out[3]:
returns
date
2022-01-04 -0.0046
2022-01-05 -0.0101
2022-01-06 -0.0102
2022-01-07 0.0009
2022-01-10 0.0045
... ...
2023-10-12 0.0095
2023-10-13 -0.0105
2023-10-16 -0.0100
2023-10-17 0.0035
2023-10-18 -0.0057

432 rows × 1 columns

In [4]:
bg = BackTestRunner(ed.EnhancingDividend, options=opts)
bg.prepare(bench=bench)
bg.adjust_signal()
bg.run()
bg.plot()
Calculating with npartitions=9
2023-10-18 14:33:08.860 | INFO     | XQuant.Utils:timer:526 - “pandas_parallelcal” run time: 0:00:32.866795.
Cache: E:\yuankangrui\XQuant\XQuant\Temp\BackTestCache\20221230-20231018-1596720896665190643
Loading close from cache ...
Loading preclose from cache ...
Loading bench from cache ...
In [5]:
bg.cache[bg.date_range_str]["bench"]
Out[5]:
returns
date
2022-12-30 0.0039
2023-01-03 0.0042
2023-01-04 0.0013
2023-01-05 0.0194
2023-01-06 0.0031
... ...
2023-10-11 0.0028
2023-10-12 0.0095
2023-10-13 -0.0105
2023-10-16 -0.0100
2023-10-17 0.0035

190 rows × 1 columns